package cn.information.view.extend;

import cn.information.bean.Purchase;
import cn.information.dao.PurchaseDao;

import javax.swing.*;
import java.awt.*;
import java.util.List;

import static com.sun.org.apache.xerces.internal.util.DOMUtil.setVisible;
import static java.awt.AWTEventMulticaster.add;

public class PurchaseQueryUI  extends JFrame {
    private JComboBox<String> nameComboBox;
    private JButton btnConfirm;
    private JButton btnCancel;

    public PurchaseQueryUI() {
        setTitle("顾客信息查询");
        setSize(400, 200);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        JPanel panelCenter = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 20));
        JPanel panelSouth = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));

        //获取所有顾客信息
        PurchaseDao purchaseDao = new PurchaseDao();
        List<Purchase> purchases = purchaseDao.findAll();
        String[] types = purchases.stream()
                .map(Purchase::getGkname)
                .distinct()
                .toArray(String[]::new);

        panelCenter.add(new JLabel("请选择要查询的顾客姓名:"));
        nameComboBox = new JComboBox<>(types);
        panelCenter.add(nameComboBox);

        btnConfirm = new JButton("确认");
        btnConfirm.setEnabled(false); // Initially disabled
        panelSouth.add(btnConfirm);

        JButton btnCancel = new JButton("取消");
        btnCancel.addActionListener(e -> dispose());
        panelSouth.add(btnCancel);

        add(panelCenter, BorderLayout.CENTER);
        add(panelSouth, BorderLayout.SOUTH);

        // Add listener to combo box
        nameComboBox.addActionListener(e -> {
            if (nameComboBox.getSelectedIndex() >= 0) {
                btnConfirm.setEnabled(true);
            } else {
                btnConfirm.setEnabled(false);
            }
        });

        // Add listener to confirm button
        btnConfirm.addActionListener(e -> {
            String selectedName = (String) nameComboBox.getSelectedItem();
            new PurchaseQueryResultUI(selectedName).setVisible(true);
            dispose();
        });

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    public static void main(String[] args) {
        new PurchaseQueryUI().setVisible(true);
    }
}




